package com;

import com.controller.MyAppInitializer;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.util.Set;

@HandlesTypes(MyAppInitializer.class)
public class MyServletContainerInitializer implements ServletContainerInitializer {
   /* @Override
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {

        ServletRegistration.Dynamic registration = ctx.addServlet("xxxx", "com.controller.SecondServlet");
        registration.addMapping("/second");
        //这里可以给其配置multipart相关内容
        //registration.setMultipartConfig(new MultipartConfigElement());
    }*/

   /* @Override
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
// 第一种
      *//*  ServletRegistration.Dynamic registration = ctx.addServlet("adf", "DispatcherServlet全称");
        registration.setInitParameter("contextClass","spring容器类的全称");
        registration.setInitParameter("contextConfigLocation", "config.AppConfig");
          *//*
    // 第二种
        DispatcherServlet servlet = new DispatcherServlet();
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(MvcConfig.class);
        servlet.setApplicationContext(context);

        ctx.addServlet("adsf", servlet)
                .addMapping("/");
    }*/


    @Override
    public void onStartup(Set<Class<?>> classes, ServletContext ctx) throws ServletException {
        for (Class<?> aClass : classes) {
            try {
                MyAppInitializer initializer = (MyAppInitializer) aClass.newInstance();
                initializer.onBegin(ctx);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
